<?php

namespace App\Transformers;

use App\Models\SinglePage;
use League\Fractal\TransformerAbstract;

class SinglePageTransformer extends TransformerAbstract
{
    protected $availableIncludes = ['user', 'content'];

    public function transform(SinglePage $page)
    {
        return $page->attributesToArray();
    }

    public function includeUser(SinglePage $page)
    {
        if (!$page->user) {
            return $this->null();
        }

        return $this->item($page->user, new UserTransformer());
    }

    public function includeContent(SinglePage $page)
    {
        if (!$page->content) {
            return $this->null();
        }

        return $this->item($page->content, new ContentTransformer());
    }
}